/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.PhieuDatChoPOJO;

/**
 *
 * @author b2
 */
public class PhieuDatChoDAO {

    public static ArrayList<PhieuDatChoPOJO> layDanhSachDatCho() {
        ArrayList<PhieuDatChoPOJO> ds = new ArrayList<PhieuDatChoPOJO>();
        try {
            String sql = "SELECT * FROM PHIEUDATCHO";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                PhieuDatChoPOJO pdc = new PhieuDatChoPOJO();
                pdc.setMaPhieuDat(rs.getInt("MaPhieuDat"));
                pdc.setMaChuyenBay(rs.getString("MaChuyenBay"));
                pdc.setMaHanhKhach(rs.getString("MaHanhKhach"));
                pdc.setMaHangVe(rs.getString("MaHangVe"));
                pdc.setGiaTien(rs.getDouble("GiaTien"));
                pdc.setNgayDat(rs.getString("NgayDat"));
                ds.add(pdc);
            }
            helper.close();

        } catch (SQLException ex) {
            Logger.getLogger(PhieuDatChoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }

    public static PhieuDatChoPOJO layPhieuDat(String maPhieuDat) {
        PhieuDatChoPOJO pdc = new PhieuDatChoPOJO();
        try {
            String sql = "SELECT * FROM PHIEUDATCHO WHERE MAPHIEUDAT = '" + maPhieuDat + "'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                pdc.setMaPhieuDat(rs.getInt("MaPhieuDat"));
                pdc.setMaChuyenBay(rs.getString("MaChuyenBay"));
                pdc.setMaHanhKhach(rs.getString("MaHanhKhach"));
                pdc.setMaHangVe(rs.getString("MaHangVe"));
                pdc.setGiaTien(rs.getDouble("GiaTien"));
                pdc.setNgayDat(rs.getString("NgayDat"));

            }
            helper.close();

        } catch (SQLException ex) {
            Logger.getLogger(PhieuDatChoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pdc;
    }

    public static boolean themPhieuDat(PhieuDatChoPOJO pdc) {
        boolean kq = false;
        String sql = String.format("INSERT INTO PHIEUDATCHO (`MaChuyenBay`,`MaHanhKhach`,`MaHangVe`,`GiaTien`,`NgayDat`) VALUES ('%s','%s','%s','%s','%s');", pdc.getMaChuyenBay(), pdc.getMaHanhKhach(), pdc.getMaHangVe(), pdc.getGiaTien(), pdc.getNgayDat());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();
        return kq;
    }
}
